from guac.instruction import GuacamoleInstruction
from guac.client import GuacamoleClient
from guac.configuration import GuacamoleConfiguration

class GuacamoleTunnel:
    def __init__(self, host, port, timeout=50, debug=False, logger=None):
        self.host = host
        self.port = port
        self.timeout = timeout
        self.debug=debug
    
    def create_tunnel(self,remote_protocol,**kwargs):
        client = GuacamoleClient(host=self.host,timeout=self.timeout,port=self.port,debug=self.debug)
        config=GuacamoleConfiguration()
        config.set_parameter("hostname",kwargs.get('remote_host'))
        config.set_parameter("port",kwargs.get('remote_port'))
        config.set_parameter('width',kwargs.get('remote_width'))
        config.set_parameter('height',kwargs.get('remote_height'))
        config.set_parameter('dpi',kwargs.get('remote_dpi'))
        if remote_protocol=='rdp':
            config.set_parameter('username',kwargs.get('remote_username'))
            config.set_parameter('password',kwargs.get('remote_password'))
            config.set_parameter("ignore-cert", "true")
            config.set_parameter("security", "any")
            config.set_parameter("resize-method", "reconnect")
        elif remote_protocol=='vnc':
            config.set_parameter('username',kwargs.get('remote_username'))
            config.set_parameter('password',kwargs.get('remote_password'))
        elif remote_protocol=='ssh':
            if kwargs.get('remote_private_key'):
                config.set_parameter("username",kwargs.get('remote_username'))
                config.set_parameter("private-key",kwargs.get('remote_private_key'))
                config.set_parameter("passphrase", kwargs.get('remote_passphrase'))
            else:
                config.set_parameter('username',kwargs.get('remote_username'))
                config.set_parameter('password',kwargs.get('remote_password'))
        client.handshake(protocol=remote_protocol,parameters=config.parameters)

        return client